<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item class="mr" label="应急预案ID" prop="planId">
        <el-input
          v-model="queryParams.planId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="预案名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="预案类型" prop="planModel">
        <el-select
          v-model="queryParams.planModel"
          placeholder="请选择"
          clearable
          style="width: 168px"
        >
          <el-option
            v-for="{ label, value } in sys_meet_plan_type"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="预案级别" prop="planRank">
        <el-select
          v-model="queryParams.planRank"
          placeholder="请选择"
          clearable
          style="width: 168px"
        >
          <el-option
            v-for="{ label, value } in sys_meet_plan_level"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="预案性质" prop="planNature">
        <el-select
          v-model="queryParams.planNature"
          placeholder="请选择"
          clearable
          style="width: 168px"
        >
          <el-option
            v-for="{ label, value } in sys_meet_plan_nature"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="步骤顺序" prop="stepOrder">
        <el-select
          v-model="queryParams.stepOrder"
          placeholder="请选择"
          clearable
          style="width: 168px"
        >
          <el-option
            v-for="{ label, value } in sys_meet_plan_order"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="负责人" prop="principalId">
        <el-select
          v-model="queryParams.principalId"
          placeholder="请选择"
          clearable
          style="width: 168px"
        >
          <el-option
            v-for="{ label, value } in principalOptions"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 90px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['emergency:plans:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['emergency:plans:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['emergency:plans:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['emergency:plans:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['emergency:plans:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="plansList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" width="80" align="center" type="index" />
      <el-table-column label="应急预案ID" align="center" prop="planId">
        <template #default="{ row }"> No.{{ row.planId }} </template>
      </el-table-column>
      <el-table-column label="预案名称" align="center" prop="planName" />
      <el-table-column label="预案类型" align="center" prop="planModel">
        <template #default="{ row }">
          <dict-tag :options="sys_meet_plan_type" :value="row.planModel" />
        </template>
      </el-table-column>
      <el-table-column
        label="预案级别"
        width="120"
        align="center"
        prop="planRank"
      >
        <template #default="{ row }">
          <dict-tag :options="sys_meet_plan_level" :value="row.planRank" />
        </template>
      </el-table-column>
      <el-table-column label="预案性质" align="center" prop="planNature">
        <template #default="{ row }">
          <dict-tag :options="sys_meet_plan_nature" :value="row.planNature" />
        </template>
      </el-table-column>
      <el-table-column label="预案附件" align="center" prop="planAccessory" />
      <el-table-column label="详细信息" align="center" prop="detailed" />
      <el-table-column label="步骤顺序" align="center" prop="stepOrder">
        <template #default="{ row }">
          <dict-tag :options="sys_meet_plan_order" :value="row.stepOrder" />
        </template>
      </el-table-column>
      <el-table-column label="步骤内容" align="center" prop="steopContent" />
      <el-table-column
        label="步骤负责人"
        width="110"
        align="center"
        prop="principalId"
      >
        <template #default="{ row }">
          <dict-tag :options="principalOptions" :value="row.principalId" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            text
            icon="View"
            @click="handleView(row)"
            v-hasPermi="['emergency:plans:detail']"
            >详细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应急预案管理对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="plansRef" label-width="110px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="应急预案ID" prop="planId">
              <el-input v-model="form.planId" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="预案名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案类型" prop="planModel">
              <el-select v-model="form.planModel" placeholder="请选择">
                <el-option
                  v-for="{ label, value } in sys_meet_plan_type"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案级别" prop="planRank">
              <el-select v-model="form.planRank" placeholder="请选择">
                <el-option
                  v-for="{ label, value } in sys_meet_plan_level"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案性质" prop="planNature">
              <el-select v-model="form.planNature" placeholder="请选择">
                <el-option
                  v-for="{ label, value } in sys_meet_plan_nature"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="步骤负责人" prop="principalId">
              <el-select v-model="form.principalId" placeholder="请选择">
                <el-option
                  v-for="{ label, value } in principalOptions"
                  :key="value"
                  :label="label"
                  :value="String(value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="预案附件" prop="planAccessory">
              <el-input v-model="form.planAccessory" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细信息" prop="detailed">
              <el-input v-model="form.detailed" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="步骤顺序" prop="stepOrder">
              <el-select v-model="form.stepOrder" placeholder="请选择">
                <el-option
                  v-for="{ label, value } in sys_meet_plan_order"
                  :key="value"
                  :label="label"
                  :value="Number(value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="步骤内容" prop="steopContentd">
              <el-input v-model="form.steopContent" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="form.remark"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 应急预案详细 -->
    <el-dialog
      title="应急预案详细"
      v-model="openView"
      width="700px"
      append-to-body
    >
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="应急预案ID：">{{
              'No.' + form.planId
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案名称：">{{ form.planName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案类型：">
              <dict-tag :options="sys_meet_plan_type" :value="form.planModel" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案级别：">
              <dict-tag :options="sys_meet_plan_level" :value="form.planRank" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案性质：">
              <dict-tag
                :options="sys_meet_plan_nature"
                :value="form.planNature"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="步骤负责人：">
              <dict-tag :options="principalOptions" :value="form.principalId" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="预案附件：">
              {{ form.planAccessory }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="步骤顺序：">
              <dict-tag
                :options="sys_meet_plan_order"
                :value="form.stepOrder"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="步骤内容：">
              {{ form.steopContent }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注：">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openView = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="emergencyPlans">
import {
  listPlans,
  getPlans,
  delPlans,
  addPlans,
  updatePlans
} from '@/api/emergency/plans'

import { principalTreeSelect } from '@/api/emergency/mats.js'

const { proxy } = getCurrentInstance()

/* 获取单位字典 */

const { sys_meet_plan_type } = proxy.useDict('sys_meet_plan_type') //预案类型
const { sys_meet_plan_level } = proxy.useDict('sys_meet_plan_level') //预案级别
const { sys_meet_plan_nature } = proxy.useDict('sys_meet_plan_nature') //预案性质
const { sys_meet_plan_order } = proxy.useDict('sys_meet_plan_order') //预案步骤

const plansList = ref([])
const open = ref(false)
const loading = ref(true)
const sns = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const principalOptions = ref([])
const openView = ref(false)
const title = ref('')
const total = ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    planId: [
      { required: true, message: '应急预案ID不能为空', trigger: 'blur' }
    ],
    planName: [
      { required: true, message: '预案名称不能为空', trigger: 'blur' }
    ],
    planModel: [
      { required: true, message: '预案类型不能为空', trigger: 'blur' }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 负责人ID树结构 */
function getPrincipalTree() {
  return principalTreeSelect().then((response) => {
    principalOptions.value = response.data
  })
}

/** 查询应急物资列表 */
function getList() {
  loading.value = true
  listPlans(queryParams.value).then((response) => {
    plansList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  form.value = {}
  proxy.resetForm('plansRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加应急预案'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const plansId = row.id || ids.value
  getPlans(plansId).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改应急预案'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const plansIds = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除序号为："' + sns.value + '"的数据项?')
    .then(function () {
      return delPlans(plansIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'system/plan/export',
    {
      ...queryParams.value
    },
    `plans_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['plansRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updatePlans(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addPlans(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 把每一行的索引放进row */
function tableRowClassName({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  sns.value = selection
    .map((item) => +item.index)
    .sort(function (a, b) {
      return a - b
    })
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}
// 获取搜索下拉选项
getPrincipalTree()

// 初始化列表
getList()
</script>
<style lang="scss" scoped>
.mr {
  margin-right: 20px;
}
.align-items {
  margin-left: 20px;
}
</style>
